using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class FormCreateNewRuleConstant : Forms.FormCreateNewBase
	{
		#region Initialization
		public FormCreateNewRuleConstant(Repository.RuleConstant item, bool bAllowContinue)
			: base(item, item, bAllowContinue)
		{
			InitializeComponent();

			this.SuspendLayout();
			try {
				if ( this.RepositoryItem != null ) {
					this.Text = string.Format(Resources.msgCreateNew, this.RepositoryItem.RepositoryItemTypeName);
				}

				DataTable dt = Global.Data.GetDataTypes();
				cboDataType.BeginUpdate();
				try {
					cboDataType.Items.Clear();
					foreach ( DataRow row in dt.Rows ) {
						cboDataType.Items.Add(row["datatype"].ToString());
					}
					cboDataType.SelectedItem = "string";
				} finally {
					cboDataType.EndUpdate();
				}
			} finally {
				this.ResumeLayout(true);
			}
			txtName.Select();
		}
		#endregion

		#region Variables

		//private Repository.Noun _item = null;
		#endregion

		#region Methods

		protected override void OnValidateNewItem(CancelEventArgs e)
		{
			DataTable dt = Global.Data.SearchRepositoryItems(
				Global.Status.CurrentDomainId, this.RepositoryItem.RepositoryItemType,
				txtName.Text, null, true);
			if ( dt != null && dt.Rows.Count > 0 ) {
				MessageBox.Show("An item with this name exists in the current domain." + Environment.NewLine + Environment.NewLine +
								"Please enter an other name.",
								this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
				txtName.Focus();
				txtName.SelectAll();
				e.Cancel = true;
			}
			base.OnValidateNewItem(e);
		}
		protected override void OnSaveClose(CancelEventArgs e)
		{
			if ( !save() ) {
				e.Cancel = true;
				return;
			}
			base.OnSaveClose(e);
		}
		protected override void OnSaveContinue(CancelEventArgs e)
		{
			if ( !save() ) {
				e.Cancel = true;
				return;
			}
			base.OnSaveContinue(e);
		}

		private bool save()
		{
			Type t = Global.Data.GetDOTNETTypeofXSDDataType(cboDataType.Text);
			object o = null;
			try {
				o = Convert.ChangeType(txtValue.Text, t);
			} catch {
				MessageBox.Show(string.Format(
					"The entered value is not of type '{0}'. Please enter a appropriate value.",
					cboDataType.Text), Resources.sRuleConstant, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				txtValue.Focus();
				txtValue.SelectAll();
				return false;
			}
			if ( o != null )
				txtValue.Text = o.ToString();

			if ( txtName.Text == "" ) {
				MessageBox.Show("Please enter a name.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				txtName.Focus();
				return false;
			}

			Repository.RuleConstant item = this.RepositoryItem as Repository.RuleConstant;
			if ( item == null )
				throw new Exception("Repository Item is not a rule constant.");

			item.Name = txtName.Text;
			item.Description = txtDesc.Text;
			item.DataType = cboDataType.SelectedItem.ToString();
			item.Value = txtValue.Text;
			item.DomainId = this.DomainID;

			return true;
		}

		#endregion

		#region Event Handlers
		private void btnOK_Click(object sender, EventArgs e)
		{
			CancelEventArgs cargs = new CancelEventArgs(false);
			OnValidateNewItem(cargs);
			if ( !cargs.Cancel ) {
				if ( this.AllowContinue )
					OnSaveContinue(new CancelEventArgs(false));
				else
					OnSaveClose(new CancelEventArgs(false));
			}
		}
		private void txtName_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if ( e.KeyCode == Keys.Enter ) {
				CancelEventArgs cargs = new CancelEventArgs(false);
				OnValidateNewItem(cargs);
				if ( !cargs.Cancel ) {
					if ( this.AllowContinue )
						OnSaveContinue(new CancelEventArgs(false));
					else
						OnSaveClose(new CancelEventArgs(false));
				}
			}
		}

		private void txtDesc_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if ( e.Control && e.KeyCode == Keys.Enter ) {
				CancelEventArgs cargs = new CancelEventArgs(false);
				OnValidateNewItem(cargs);
				if ( !cargs.Cancel ) {
					if ( this.AllowContinue )
						OnSaveContinue(new CancelEventArgs(false));
					else
						OnSaveClose(new CancelEventArgs(false));
				}
			}
		}

		#endregion

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

	}
}